﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ScopePropertyTestView.xaml.cs" company="JDML Consulting Ltd.">
//   Copyright 2011 Photon Project - http://photon.codeplex.com
// </copyright>
// <summary>
//   Defines the ScopePropertyTestView type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Photon.Windows
{
    using System;
    using System.Collections.Generic;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Media;

    public partial class ScopePropertyTestView : UserControl
    {
        private ScopePropertyObserver _xamlObserver;

        public ScopePropertyTestView()
        {
            ObserverLog = new List<object>();
            InitializeComponent();
            
            ObserverLog = new List<object>();
            XamlObserverLog = new List<object>();
            
            Observer = new ScopePropertyObserver(Observed, 
                With_ScopeProperty.TestValueScopeProperty);
            Observer.ValueChanged += (s, e) => ObserverLog.Add(Observer.Value);

            Loaded += (s, e) => IsLoaded = true;
            Unloaded += (s, e) => IsLoaded = false;

            Observed.Loaded += (s, e) => IsObservedLoaded = true;
            Observed.Unloaded += (s, e) => IsObservedLoaded = false;
        }

        public FrameworkElement FindName(FrameworkElement element, string name)
        {
            if (element == null)
            {
                return null;
            }
            
            var count = VisualTreeHelper.GetChildrenCount(element);
            for (var i=0; i<count; i++)
            {
                var childElement = VisualTreeHelper.GetChild(element, i);
                
                var frameworkElement = childElement as FrameworkElement;
                if (frameworkElement.Name == name)
                {
                    return frameworkElement;
                }
                
                var frameworkelement = FindName(childElement as FrameworkElement, name);
                if (frameworkelement != null)
                {
                    return frameworkelement;
                }
            }
            return null;
        }

        public bool IsXamlContentLoaded
        {
            get
            {
                var xamlObservered = FindName(XamlContent, "XamlObserved");
                if (xamlObservered != null)
                {
                    XamlObserver = XamlObserver ?? new ScopePropertyObserver(xamlObservered,
                        With_ScopeProperty.TestValueScopeProperty);
                    return true;
                }
                return false;
            }
        }

        private void OnXamlObserverOnValueChanged(object s1, EventArgs e1)
        {
            if (XamlObserver != null)
            {
                XamlObserverLog.Add(XamlObserver.Value);
            }
        }

        public ScopePropertyObserver Observer { get; private set; }

        public ScopePropertyObserver XamlObserver
        {
            get { return _xamlObserver; }
            private set
            {
                if (_xamlObserver != value)
                {
                    if (_xamlObserver != null)
                    {
                        _xamlObserver.ValueChanged -= OnXamlObserverOnValueChanged;
                    }
                    _xamlObserver = value;
                    if (_xamlObserver != null)
                    {
                        _xamlObserver.ValueChanged -= OnXamlObserverOnValueChanged;
                        if (_xamlObserver.Value != null)
                        {
                            XamlObserverLog.Add(XamlObserver.Value);
                        }
                    }
                }
            }
        }

        public List<object> ObserverLog { get; private set; }

        public List<object> XamlObserverLog { get; private set; }

        public bool IsLoaded { get; private set; }

        public bool IsObservedLoaded { get; private set; }
    }
}
